/** 设置localStorage */
function setLocalStorage(name: string, data?: unknown) {
    if (typeof data === 'undefined' || data === null) {
        localStorage.removeItem(name);
    }
    localStorage.setItem(name, JSON.stringify(data));
}

/** 读取localStorage */
function getLocalStorage<T>(name: string): T | null {
    const localVal = localStorage.getItem(name);
    if (localVal === 'undefined' || localVal === null) {
        return null;
    }
    return JSON.parse(localVal);
}

/** 清除所有localStorage */
function clearAll() {
    window.localStorage.clear();
}

export default { setLocalStorage, getLocalStorage, clearAll };
